<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 路线管理控制器
 *
 * @desc    路线管理
 * 
 * @desc    完善文档注释
 * @version 1.1.0
 * @date	2017年6月27日 下午6:01:33
 * 
 * @title 我的路线管理
 * @entry http://discovery.segway.com
 */
class Touroute extends MY_Business_Controller
{
	/** @var Lib_touroute */
	public $lib_touroute = null;
	
	public function __construct()
	{
		parent::__construct();
		
		$this->load->library('lib_touroute');
		
		$this->set_mutable_view_var('head.title', 'Tour - Business Management');
		$this->set_mutable_view_var('head.keywords', '');
		$this->set_mutable_view_var('head.description', '');
		
		$this->lang->load('touroute');
		
		// 供资源所有权验证用
		$this->member_id = @$this->get_logined_member()['id'];
		$this->merchant_id = @$this->get_logined_member()['MY_MERCHANT']['id'];
	}
	
	/**
	 * 首页 -> 列表页
	 *
	 * @return html
	 *
	 * @command pass noneurl
	 */
	public function index()
	{
		$this->listing();
		// $this->load->view('business/touroute/index');
	}
	
	/**
	 * 路线列表页
	 *
	 * @author  Jason
	 * @date	2017年5月23日 下午6:32:42
	 * @link    /business/touroute/listing
	 */
	public function listing($page = 1, $number = 20)
	{
		$cookie_lang = $this->lib_cookie->get_language();
		$language = $this->lib_language->get_supported_language($this->language, $cookie_lang);
		
		$conditions = $this->lib_touroute->get_conditions();
		$merchant_id = @$this->get_logined_member()['MY_MERCHANT']['id'];
		$conditions[] = ['where', "merchant_id=$merchant_id"];
		$list = $this->lib_touroute->get_touroutes_for_manage_list($conditions, $language, $page, $number);
		$data['list'] = $list;
		
		$this->load->view('business/touroute/list', $data);
	}
	
	/**
	 * 路线上下架
	 *
	 * @method  POST
	 * @author  Jason
	 * @date    2017年5月24日 上午10:26:52
	 * 
	 * @param   number $id
	 * 
	 * @return  JSON	标准JSON格式响应
	 * 
	 * @link    /business/touroute/ajax_toggle_shelve
	 * 
	 * @command noneurl
	 */
	public function ajax_toggle_shelve()
	{
		$id = $this->input->post_get('id');
		list($status, $content) = $this->lib_touroute->before_manage_validation($id, $this->member_id, $this->merchant_id);
		if ($status) {
			list($status, $content) = $this->lib_touroute->toggle_status_enabled($id);
		}
		
		if(!$status) {
			Lib_json::renderError(Lib_json::ERROR_BUSINESS, current($content));
		} else {
			Lib_json::renderSuccess(['data' => $content]);
		}
	}
	
	/**
	 * 路线详情 - 编辑前信息载入
	 * 
	 * @desc    编辑前信息载入
	 * @method  GET
	 * @author  Jason
	 * @date	2017年5月24日 下午4:23:42
	 * 
	 * @param   number $id       ID，默认为 0，代表新增一条记录
	 * @param   string $language 默认为 ""，从 COOKIE 中取，取不到则默认为 <default>"english"
	 * 			把参数按顺序辍到 URL 之后，用 "/" 间隔
	 * @example 如果参数为 id=45 language=chinese
	 * 			那么URL为 /business/touroute/detail/45/chinese
	 * 
	 * @demoParam id 45
	 * @demoParam language chinese
	 */
	public function detail($id = 0, $language = '')
	{
		$cookie_lang = $this->lib_cookie->get_language();
		$language = $this->lib_language->get_supported_language($language, $cookie_lang);
		$currency = $this->lib_cookie->get_currency();
		
		$touroute = $this->lib_touroute->get_touroute_for_detail($id, $language, $currency);
		if (empty($touroute)) {
			$touroute = [];
		}
		
		$touroute_periods = $this->lib_touroute->get_touroute_periods_for_detail($id);
		$this->load->helper('array');
		
		$this->load->library('lib_section');
		$data['content']['search'] = $this->lib_section->index_search();
		$data['content']['language'] = $language;
		$data['content']['touroute'] = $touroute;
		$data['content']['touroute_periods'] = $touroute_periods;
		
		$this->load->view('business/touroute/detail', $data);
	}
	
	/**
	 * 路线语言详情 - 编辑语言前信息载入
	 *
	 * @desc    编辑语言前信息载入
	 * @method  GET
	 * @author  Jason
	 * @date	2017年5月25日 下午2:55:56
	 *
	 * @param   number $id
	 * @param   string $language 默认为 ""，从 COOKIE 中取，取不到则默认为 <default>"english"
	 * 			把参数按顺序辍到 URL 之后，用 "/" 间隔
	 * @example 如果参数为 id=45 language=chinese
	 * 			那么URL为 /business/touroute/detail_language/45/chinese
	 * 
	 * @demoParam id 45
	 * @demoParam language chinese
	 */
	public function detail_language($id, $language = '')
	{
		$cookie_lang = $this->lib_cookie->get_language();
		$language = $this->lib_language->get_supported_language($language, $cookie_lang);
		
		// 获取语言
		$resource = $id;
		$module = [$this->mod_touroute, 'master'];
		
		$data = $this->lib_language->select_language($resource, $module, $language);
		$data['content']['id'] = $id;
		$data['content']['language'] = $language;
		$data['content']['values'] = $data['values'];
		
		$this->load->view('business/touroute/detail_language', $data);
	}
	
	/**
	 * 路线详情编辑提交 - 编辑完后的信息提交 - 增加 & 修改
	 * 
	 * @desc    编辑完后的信息提交 - 增加 & 修改
	 * @method  POST
	 * @author  Jason
	 * @date	2017年5月24日 下午5:39:14
	 * 
	 * @param   number $id		 路线ID，默认 0，代表添加一个新路线
	 * @param   string $language 语言标识，必填项！
	 * ...
	 * 
	 * @return  JSON	标准JSON格式响应
	 * 					结构：无
	 * 
	 * @link    /business/touroute/ajax_create
	 * 
	 * @command noneurl
	 */
	public function ajax_create()
	{
		// 检测商户是否审核
		$this->lang->load('merchant');
		$this->load->library('lib_merchant');
		list($status, $content) = $this->lib_merchant->check_status($this->merchant_id);
		
		if(!$status) {
			Lib_json::renderError(Lib_json::ERROR_BUSINESS, current($content));
			return false;
		}
		
		$_POST['merchant_id'] = $this->merchant_id;
		if (empty($id = $this->input->post('id'))) {
			list($status, $content) = $this->lib_touroute->create();
			if ($status) {
				// MARK touroute content is too large!
				$this->load->helper('array');
				$content = arrayColumn($content, ['id']);
			}
		} else {
			list($status, $content) = $this->lib_touroute->before_manage_validation($id, $this->member_id, $this->merchant_id);
			if ($status) {
				list($status, $content) = $this->lib_touroute->update();
			}
		}
		
		if(!$status) {
			$messageContent = Lib_json::getMessageContent($content);
			Lib_json::renderError(Lib_json::ERROR_BUSINESS, current($content), $messageContent);
		} else {
			Lib_json::renderSuccess(['data' => $content], 'success');
		}
	}
	
	/**
	 * 路线多语言信息编辑提交
	 *
	 * @desc    编辑完后的信息提交 - 增加 & 修改
	 * @method  POST
	 * @author  Jason
	 * @date	2017年5月25日 下午3:36:47
	 *
	 * @param   number $resource 路线ID，必填项！
	 * @param   string $language 语言标识，必填项！
	 * ...
	 *
	 * @return  JSON	标准JSON格式响应
	 * 					结构：无
	 *
	 * @link    /business/touroute/ajax_create_language
	 * 
	 * @command noneurl
	 */
	public function ajax_create_language()
	{
		if (empty($resource = $this->input->post('resource'))) {
			list($status, $content) = $this->lib_touroute->create_language();
		} else {
			list($status, $content) = $this->lib_touroute->before_manage_validation($resource, $this->member_id, $this->merchant_id);
			if ($status) {
				list($status, $content) = $this->lib_touroute->create_language();
			}
		}
		
		if(!$status) {
			$messageContent = Lib_json::getMessageContent($content);
			Lib_json::renderError(Lib_json::ERROR_BUSINESS, current($content), $messageContent);
		} else {
			Lib_json::renderSuccess(['data' => $content], 'success');
		}
	}
}
